package org.thread.test04.demo10.homework.demo02;

import java.util.Random;

/**
 *
 * @author CSY
 * @date 2025 - 03 - 11
 */
public class MyRunnable implements Runnable{

    /**
     * 总票数
     */
    private Integer ticketCount;

    public MyRunnable(){}

    public MyRunnable(Integer ticketCount) {
        this.ticketCount = ticketCount;
    }
    @Override
    public void run() {
        while (true) {
            Integer sellCount = new Random().nextInt(1,10);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if(ticketCount ==0){
                System.out.println(Thread.currentThread().getName()+"没票了，停止售票");
                break;
            }
            if (sellCount <= ticketCount) {
                ticketCount-=sellCount;
                System.out.println(Thread.currentThread().getName()+"成功出售"+sellCount+"张票，剩余："+ticketCount);
            }else{
                sellCount = ticketCount;
            }
        }
    }
}
